package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.ReportJlXjlgsjlbBaDetail;
import com.ruoyi.report_jl.domain.ReportJlXjlssjlbBaDetail;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlssjlbBaDetailVo;
import com.ruoyi.report_jl.mapper.ReportJlXjlssjlbBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlXjlssjlbBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlXjlssjlbBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlXjlssjlbBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlXjlssjlbBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlXjlssjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 细集料筛分试验检测记录（水筛法）Service业务层处理
 *
 * @author tll
 * @date 2024-08-05
 */
@RequiredArgsConstructor
@Service
public class ReportJlXjlssjlbBsInfoServiceImpl implements IReportJlXjlssjlbBsInfoService {

    private final ReportJlXjlssjlbBsInfoMapper baseMapper;
    private final ReportJlXjlssjlbBaDetailMapper reportJlXjlssjlbBaDetailMapper;

    /**
     * 查询细集料筛分试验检测记录（水筛法）
     */
    @Override
    @FIllBasicInfo
    public ReportJlXjlssjlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportJlXjlssjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportJlXjlssjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportJlXjlssjlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportJlXjlssjlbBaDetail::getCreateTime);
        List<ReportJlXjlssjlbBaDetailVo> detailList = reportJlXjlssjlbBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询细集料筛分试验检测记录（水筛法）列表
     */
    @Override
    public TableDataInfo<ReportJlXjlssjlbBsInfoVo> queryPageList(ReportJlXjlssjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlXjlssjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlXjlssjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询细集料筛分试验检测记录（水筛法）列表
     */
    @Override
    public List<ReportJlXjlssjlbBsInfoVo> queryList(ReportJlXjlssjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlXjlssjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlXjlssjlbBsInfo> buildQueryWrapper(ReportJlXjlssjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlXjlssjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlXjlssjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlXjlssjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlXjlssjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlXjlssjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlXjlssjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlXjlssjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlXjlssjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlXjlssjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlXjlssjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlXjlssjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlXjlssjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlXjlssjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlXjlssjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlXjlssjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlXjlssjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlXjlssjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlXjlssjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlXjlssjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlXjlssjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlXjlssjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlXjlssjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlXjlssjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlXjlssjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlXjlssjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getShaifhzzlItem1() != null, ReportJlXjlssjlbBsInfo::getShaifhzzlItem1, bo.getShaifhzzlItem1());
        lqw.eq(bo.getKoucshsyzzlItem1() != null, ReportJlXjlssjlbBsInfo::getKoucshsyzzlItem1, bo.getKoucshsyzzlItem1());
        lqw.eq(bo.getSunhlItem1() != null, ReportJlXjlssjlbBsInfo::getSunhlItem1, bo.getSunhlItem1());
        lqw.eq(bo.getShaifhzzlItem2() != null, ReportJlXjlssjlbBsInfo::getShaifhzzlItem2, bo.getShaifhzzlItem2());
        lqw.eq(bo.getKoucshsyzzlItem2() != null, ReportJlXjlssjlbBsInfo::getKoucshsyzzlItem2, bo.getKoucshsyzzlItem2());
        lqw.eq(bo.getSunhlItem2() != null, ReportJlXjlssjlbBsInfo::getSunhlItem2, bo.getSunhlItem2());
        lqw.eq(bo.getSunhItem1() != null, ReportJlXjlssjlbBsInfo::getSunhItem1, bo.getSunhItem1());
        lqw.eq(bo.getSunhItem2() != null, ReportJlXjlssjlbBsInfo::getSunhItem2, bo.getSunhItem2());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem1()), ReportJlXjlssjlbBsInfo::getChenglcsItem1, bo.getChenglcsItem1());
        lqw.eq(bo.getZhil3Item1() != null, ReportJlXjlssjlbBsInfo::getZhil3Item1, bo.getZhil3Item1());
        lqw.eq(bo.getZhil4Item1() != null, ReportJlXjlssjlbBsInfo::getZhil4Item1, bo.getZhil4Item1());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem2()), ReportJlXjlssjlbBsInfo::getChenglcsItem2, bo.getChenglcsItem2());
        lqw.eq(bo.getZhil3Item2() != null, ReportJlXjlssjlbBsInfo::getZhil3Item2, bo.getZhil3Item2());
        lqw.eq(bo.getZhil4Item2() != null, ReportJlXjlssjlbBsInfo::getZhil4Item2, bo.getZhil4Item2());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportJlXjlssjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportJlXjlssjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportJlXjlssjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(bo.getShuix075mItem1() != null, ReportJlXjlssjlbBsInfo::getShuix075mItem1, bo.getShuix075mItem1());
        lqw.eq(bo.getShuix075pItem1() != null, ReportJlXjlssjlbBsInfo::getShuix075pItem1, bo.getShuix075pItem1());
        lqw.eq(bo.getShuix075mItem2() != null, ReportJlXjlssjlbBsInfo::getShuix075mItem2, bo.getShuix075mItem2());
        lqw.eq(bo.getShuix075pItem2() != null, ReportJlXjlssjlbBsInfo::getShuix075pItem2, bo.getShuix075pItem2());
        lqw.eq(bo.getShuix075pjz() != null, ReportJlXjlssjlbBsInfo::getShuix075pjz, bo.getShuix075pjz());
        return lqw;
    }

    /**
     * 新增细集料筛分试验检测记录（水筛法）
     */
    @Override
    public Boolean insertByBo(ReportJlXjlssjlbBsInfoBo bo) {
        ReportJlXjlssjlbBsInfo add = BeanUtil.toBean(bo, ReportJlXjlssjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改细集料筛分试验检测记录（水筛法）
     */
    @Override
    public Boolean updateByBo(ReportJlXjlssjlbBsInfoBo bo) {
        ReportJlXjlssjlbBsInfo update = BeanUtil.toBean(bo, ReportJlXjlssjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            reportJlXjlssjlbBaDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportJlXjlssjlbBaDetail.class));
        }
        return flag;
//        boolean upd = baseMapper.updateById(update) > 0;
//        boolean upd2 = false;
//        boolean insert = false;
//        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportJlXjlssjlbBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportJlXjlssjlbBaDetail> list = reportJlXjlssjlbBaDetailMapper.selectList(queryWrapper);
//            for (ReportJlXjlssjlbBaDetail det : bo.getDetailList()) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = reportJlXjlssjlbBaDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = reportJlXjlssjlbBaDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlXjlssjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除细集料筛分试验检测记录（水筛法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
